package com.whlilong.jxc.ui;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;

import com.whlilong.jxc.constants.IAppConstants;
import com.whlilong.jxc.model.Regist;
import com.whlilong.jxc.model.Users;
import com.whlilong.jxc.sys.CacheImage;
import com.whlilong.jxc.sys.ExtentionInfo;
import com.whlilong.jxc.sys.IImageKey;
import com.whlilong.jxc.sys.SaveLogInfo;

public class RegistDialog extends Dialog {
	private Text registcodeText;
	private int regist = 8888;

	private List<Users> userVector = new ArrayList<Users>();
	private GridData gd_registcodeText;
	private Label date;
	private String month;
	private String day;;

	public RegistDialog(Shell parentShell) {
		super(parentShell);
		// TODO Auto-generated constructor stub
	}

	protected Control createDialogArea(Composite parent) {
		// create a composite with standard margins and spacing
		Composite container = (Composite) super.createDialogArea(parent);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		gridLayout.marginRight = 6;
		gridLayout.makeColumnsEqualWidth = false;
		container.setLayout(gridLayout);
		GridData gd = new GridData(GridData.FILL_BOTH);
		container.setLayoutData(gd);

		final CLabel title = new CLabel(container, SWT.CENTER);
		title.setFont(new Font(Display.getCurrent(), "隶书", 12, SWT.BOLD
				| SWT.ITALIC));
		title.setText("欢迎使用立隆商务信息管理系统");
		title.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.title));
		gd = new GridData(GridData.FILL_BOTH);
		gd.horizontalSpan = 3;
		title.setLayoutData(gd);

		Label label = new Label(container, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label.setText("试用期：");
		Label label_1 = new Label(container, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false,
				false, 1, 1));
		label_1.setText("30天");
		new Label(container, SWT.NONE);

		final CLabel label2 = new CLabel(container, SWT.RIGHT);
		label2.setText("注册店名：");
		// label2.setImage(CacheImage.getINSTANCE().getImage(
		// IAppConstants.APPLICATION_ID, IImageKey.PASSWORD));
		gd = new GridData(GridData.FILL_BOTH);
		gd.widthHint = 100;
		label2.setLayoutData(gd);
		registcodeText = new Text(container, SWT.BORDER);
		gd_registcodeText = new GridData(GridData.FILL_HORIZONTAL);
		gd_registcodeText.widthHint = 160;
		registcodeText.setLayoutData(gd_registcodeText);
		registcodeText.setFocus();
		new Label(container, SWT.NONE);

		date = new Label(container, SWT.NONE);
		date.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1,
				1));
		List<Regist> list = ExtentionInfo.getInstance().getRegist();
		String pas = list.get(0).getPass();
		if (!pas.equals("") && pas != null) {
			String[] str = pas.split("-");
			int str1 = Integer.parseInt(str[0]) / 27;
			int str2 = Integer.parseInt(str[2]) / 12;
			if (str2 > 9)
				month = str2 + "";
			if (str2 < 10 && str2 >= 0) {
				month = "0" + str2;
			}
			int str3 = Integer.parseInt(str[4]) / 15;
			if (str3 > 9)
				day = str3 + "";
			if (str3 < 10 && str3 >= 0) {
				day = "0" + str3;
			}
			pas = str1 + "-" + month + "-" + day;
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date passDate = null;
			try {
				Calendar c = Calendar.getInstance();
				passDate = df.parse(pas);
				c.setTime(passDate);
				c.add(c.DATE, 30);
				passDate = c.getTime();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			date.setText(df.format(passDate));
		}
		Label label_3 = new Label(container, SWT.NONE);
		label_3.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false,
				false, 1, 1));
		label_3.setText("过期");
		new Label(container, SWT.NONE);

		return container;
	}

	protected void createButtonsForButtonBar(final Composite parent) {
		// create OK and Cancel buttons by default
		Button button = createButton(parent, IDialogConstants.OK_ID, "注册", true);
		Button button_1 = createButton(parent, IDialogConstants.CANCEL_ID,
				"试用", false);
	}

	protected void okPressed() {
		String registcode = registcodeText.getText();
		SaveLogInfo.getInstance().setRegistcode(registcode);
		super.okPressed();
	}

	protected Point getInitialSize() {
		return new Point(350, 200);
	}

	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("软件注册");
		// newShell.setImage(CacheImage.getINSTANCE().getImage(
		// IAppConstants.APPLICATION_ID, IImageKey.WINDOW_IMAGE));
	}

}
